import { Controller, Post, Body, Get, Query, Param } from '@nestjs/common';
import { PostService } from './post.service';
import { CreatePostDto } from './dto/create-post.dto';

import {
  GetUserInfo,
  RequrieLogin,
  RequriePermission,
} from 'src/cu-dec/cu-dec';

@Controller('post')
@RequrieLogin()
export class PostController {
  constructor(private readonly postService: PostService) {}

  @Post()
  @RequriePermission('create')
  create(
    @Body() createPostDto: CreatePostDto,
    @GetUserInfo('id') userId: number,
  ) {
    return this.postService.create(createPostDto, +userId);
  }

  @Get()
  findAll(
    @Query('categoryId') categoryId: number,
    @Query('page') page: number,
    @Query('limit') limit: number,
    @Query('keyword') keyword: string,
  ) {
    return this.postService.findAll(+categoryId, +page, +limit, keyword);
  }

  @Get('category')
  getCategoryList() {
    return this.postService.getCategoryList();
  }

  @Get('readNum')
  readNum(@Query('id') id: number, @GetUserInfo('id') userId: number) {
    return this.postService.readNum(+id, +userId);
  }
  @Get('support')
  support(@Query('id') id: number, @GetUserInfo('id') userId: number) {
    return this.postService.support(+id, +userId);
  }

  // 取消点赞
  @Get('cancelSupport')
  cancelSupport(@Query('id') id: number, @GetUserInfo('id') userId: number) {
    console.log(id, 'id');
    return this.postService.cancelSupport(+id, +userId);
  }

  // 获取文章详情
  @Get(':id')
  getPostDetail(@Param('id') id: number) {
    console.log(id);
    return this.postService.getPostDetail(+id);
  }
}
